package com.yanqu.road.server.gameplayer.module.activity.jinli;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.jinli.config.JinLiConfig;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.pb.activity.JinLiProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.jinli.JinLiMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.Protocol;


/**
 * 锦鲤签
 */
public class JinLiModule extends GeneralModule {

    public JinLiModule(GamePlayer player) {
        super(player);
    }

    @Override
    public void afterLogin() {
        // 登录下发
        initSystem();
    }

    public void syncData() {
        JinLiConfig config = JinLiMgr.getConfig();
        if (config == null) {
            return;
        }
        ActivityInfo activityInfo = config.getActivityInfo();
        if (!ActivityHelper.activityInShowTime(activityInfo)) {
            return;
        }
        // 上跨服拉数据
        JinLiProto.CrossJinLiSyncDataMsg.Builder builder = JinLiProto.CrossJinLiSyncDataMsg.newBuilder();
        builder.setActivityId(activityInfo.getActivityId());
        player.sendPacket(Protocol.C_CROSS_JINLI_SYNC_DATA, builder);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    /**
     * 锦鲤充值
     *
     * @param activityId
     * @param mallInfo
     */
    public void recharge(int activityId, MallInfo mallInfo) {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.JinLi.getValue())) {
            log.error("系统未解锁，活动id:{}", activityId);
            return;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            log.error("锦鲤活动不存在，活动id:{}", activityId);
            return;
        }
        if (!ActivityHelper.activityInShowTime(activityInfo)) {
            log.error("锦鲤活动不在展示期，活动id:{}", activityId);
            return;
        }
        // 上跨服抽签
        JinLiProto.CrossAddJinLiReqMsg.Builder builder = JinLiProto.CrossAddJinLiReqMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setMallId(mallInfo.getMallId());
        builder.setPlayerInfo(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
        player.sendPacket(Protocol.C_CROSS_JINLI_ADD, builder);
    }

    /**
     * 判断是否能充值
     */
    public boolean canCharge(int mallId) {
        // 获取锦鲤签活动
        JinLiConfig config = JinLiMgr.getConfig();
        if (config == null) {
            return false;
        }
        ActivityInfo activityInfo = config.getActivityInfo();
        if (!ActivityHelper.activityInShowTime(activityInfo)) {
            return false;
        }
        // 判断下等级是否能购买
        return player.getUserInfo().getTitleId() >= config.getLvLimit();
    }

    public void initSystem() {
        // 身份解锁
        if (!SystemOpenMgr.systemOpen(player, eSystemId.JinLi.getValue())) {
            return;
        }
        syncData();
    }
}
